/***2024-12-19 迷途小羔羊
* 用于编写ai逻辑基类
*/
namespace ghost
{
    export class AIBase{        
        public static create(type:AIEnum, data:ISmartParam):AIBase
        {
            if(type == AIEnum.MMOSmart)
                return new MMOSmart(data);                                    
            if(type == AIEnum.TargetSmart)
                return new TargetSmart(data);
            if(type == AIEnum.TowerSmart)
                return new TowerSmart(data);
            
            return null;
        }
        /**当前ai行为列表*/protected _behaviorList:BehaviorBase[];
        /**当前ai行为类型*/protected _behaviorType:BehaviorEnum;
        /**当前ai行为对象*/protected _behavior:BehaviorBase;
        /**ai数据*/public _data:ISmartParam;
        /**上次执行智能的时间*/protected _smartTime:number;
        constructor(data:ISmartParam)
        {
            let s = this;
            s._data = data;
            s._behaviorList = [];
            s._behavior = null;
            s._behaviorType = BehaviorEnum.NONE;            
            s.behaviorInit();
        }
        protected behaviorInit():void{}
        /**设置当前执行的行为
         * @param type 行为类型
        */
        public setBehavior(type:BehaviorEnum):void
        {
            let s = this;
            s._behavior = s._behaviorList[type];
            s._behaviorType = type;
            if(type > -1 && s._behavior == null)
            {
                // Log.writeLog("执行未预设的行为!行为id:" + type + ",角色：" + JSON.stringify(s._data.role.cfg), Log.ERROR);
                debugger;
            }                
        }
        /**添加多个行为
         * @param types 行为类型数组
        */
        public addBehaviors(types:BehaviorEnum[]):void
        {
            let i:number,len:number;
            let s = this;
            len = types.length;
            for(i=0;i<len;++i)            
                s.addBehavior(types[i], false);
        }
        /**添加行为
         * @type type 行为类型 
         * @param setAsCur 是否设置为当前执行行为
        */
        public addBehavior(type:BehaviorEnum, setAsCur:boolean=true):void
        {
            let s = this;   
            let cls:any;     
            if(type == BehaviorEnum.TARGET)
                cls = B_Target;
            else if(type == BehaviorEnum.ATTACK)
                cls = B_FindAttack;            
            else if(type == BehaviorEnum.DETOUR)
                cls = B_Detour;            
            else if(type == BehaviorEnum.TOWER_ATTACK)
                cls = B_TowerAttack;        
            if(cls == null)    
                return;
            s._behaviorList[type] = new cls(s);            
            if(setAsCur)
                s.setBehavior(type);
        }
        public baseSmart(t:number):void
        {
            let s = this;
			if(s._behavior)
                s._behavior.exc(t);
        }
        /**普通挂机智能*/
		public smart(t:number):void
		{            
			this.baseSmart(t);
		}
        /**进入智能*/
        public inSmart(): void
        {
            let s = this;
            let len:number;
            len = s._behaviorList.length;
            while(--len>-1)
                s._behaviorList[len] && s._behaviorList[len].resetState();
        }
        /**离开智能*/
        public outSmart(): void {            
            let s = this;
            let len:number;
            len = s._behaviorList.length;
            while(--len>-1)            
                s._behaviorList[len] && s._behaviorList[len].clear();
            this._behaviorType = BehaviorEnum.NONE;
        }        
        /**通知ai目标变更*/
        public targetChange():void
        {

        }
        public dispose():void
        {
            let len:number;
            let s = this;
            len = s._behaviorList.length;
            while(--len>-1)
            {
                s._behaviorList[len] && s._behaviorList[len].dispose();
            }
            s._behaviorList.length = 0;
            s._behavior = null;
        }
        /**行为结束*/
        public behaviorOut(b:BehaviorBase, nextBehavior:BehaviorEnum):void
        {
            
        }
    }
    export interface ISmartParam
    {
        /**ai控制的角色*/role:GameRole;
        /**ai参数*/param?:IAIParam;
    }
    export interface IAIParam
    {
        behaviorParams:IBehaivorParam[];
        [key:string]:any;
    }
    export interface IBehaivorParam
    {
        
    }
}